
package restPROC;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
 




import utilClasses.ListaTipVozila;
import utilClasses.ListaVozila;
import dao.daoKonekcija;
import dao.daoPermisije;
import dao.daoVozilo;



@Path("/vozila")
public class Vozilo {
	@POST
	@Path("/unos")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("text/plain")
	public String unesiVozilo(@FormParam("vozilo") String vozilo,@FormParam("gsirina") String gsirina, @FormParam("gduzina") String gduzina,@FormParam("nazivLinije") String nazivLinije,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modvozilo");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			//daoVozilo.unesiVozilo("autobus", "adfaf",12, 34, "test");
			daoVozilo.unesiVozilo(vozilo,  Double.parseDouble(gsirina), Double.parseDouble(gduzina), nazivLinije);
			
			return "true";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "false";
	}
	
	@GET
	@Path("/dajImena")
	@Produces({"application/xml"})
	public ListaTipVozila dajImena()
	{
		ListaTipVozila rez=new ListaTipVozila();
		try {
			
			rez=daoVozilo.dajImenaVozila();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rez;
	}	
	
	

	
	

	
	@POST
	@Path("/listavozila")
	@Produces({"application/xml"})
	public ListaVozila getVozila()
	{
		ListaVozila rez = new ListaVozila();
		try {
			
			rez=daoVozilo.dajVozila();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}

	@POST
	@Path("/brisanje")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("text/plain")
	public String deleteVozilo(@FormParam("idVozila") String idVozila,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modvozilo");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			daoVozilo.brisiVozilo(idVozila);
			
			
			return "true";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "false";
	}
	
	
}